define(['app', 'json2'], function(app, JSON2) {
    app.controller('EditMockController', function($scope, $http, xContext, $uibModalInstance, xDialog, xUtil, toastr) {
        var self = this;
        self.module = '';
        self.type = '';
        self.parent = '';
        self.folder = '';
        self.file = '';
        self.modifiable = true;
        self.method = '';
        self.extname = '';
        self.oldname = '';
        self.changed = true;

        self.init = function() {
            self.module = xContext.module;
            self.modifiable = xContext.modifiable;

            // Reset form
            var nodeName = (xContext.treeNode.oldname || xContext.treeNode.name);
            if (self.modifiable) {
                if (xContext.treeNode.root) {
                    self.parent = '/';
                } else {
                    self.parent = xUtil.path.join([xContext.treeNode.parent, nodeName]);
                }
            } else {
                if (nodeName.indexOf('/') == 0) {
                    var pathIndex = nodeName.lastIndexOf('/');
                    self.parent = nodeName.substring(0, pathIndex);
                    nodeName = nodeName.substring(pathIndex + 1);
                }
                var dotIndex = nodeName.lastIndexOf('.');
                if (dotIndex == -1) {
                    self.type = 'folder';
                    self.folder = nodeName;
                } else {
                    self.extname = nodeName.substring(dotIndex);
                    if (self.extname) {
                        self.type = 'file';
                        nodeName = nodeName.substring(0, dotIndex);
                        dotIndex = nodeName.lastIndexOf('.');
                        if (dotIndex != -1) {
                            self.method = nodeName.substring(dotIndex);
                        }
                        self.file = dotIndex == -1 ? nodeName : nodeName.substring(0, dotIndex);
                    }
                }
                self.oldname = self.folder || (self.file + self.method);
                self.parent = self.parent || xContext.treeNode.parent || '/';
            }

            $scope.$watch('dvm.extname', function(newValue) {
                if (newValue != '.json' && newValue != '.xml') {
                    self.method = '';
                }
            })

            $scope.$watchGroup(['dvm.folder', 'dvm.file', 'dvm.method'], function(newValue) {
                if (self.type == 'folder') {
                    self.changed = (self.oldname != newValue[0]);
                } else {
                    self.changed = (self.oldname != newValue[1] + newValue[2]);
                }
            })
        }

        self.save = function(valid) {
            if (valid) {
                var filename = self.file + self.method + self.extname;
                if (self.modifiable) {
                    var folder = self.parent;
                    if (self.type == 'folder') {
                        folder = xUtil.path.join([self.parent, self.folder]);
                    }
                    $http.post('/mock/data/save', {
                        created: true,
                        module: self.module,
                        type: self.type,
                        folder: folder,
                        file: filename,
                        data: (self.extname == '.json' ? '{}' : (self.extname == '.xml' ? '<xml/>' : ''))
                    }).then(function(response) {
                        if (response.data == 'required') {
                            xDialog.alert('提示信息', '数据模块【' + self.module + '】不存在！');
                        } else if (response.data) {
                            if (self.type == 'file') {
                                toastr.success('文件【' + filename +'】已添加！');
                            } else {
                                toastr.success('文件夹【' + self.folder + '】已添加！');
                            }
                            $uibModalInstance.close({
                                name: (self.type == 'file' ? filename : self.folder),
                                leaf: (self.type == 'file'),
                                parent: (self.parent.root ? '' : self.parent)
                            });
                        } else if (response.status == 200) {
                            if (self.type == 'file') {
                                xDialog.alert('提示信息', '您创建的文件已存在！');
                            } else {
                                xDialog.alert('提示信息', '您创建的文件夹已存在！');
                            }
                        }
                    });
                } else {
                    var nodeName = (xContext.treeNode.oldname || xContext.treeNode.name);
                    var isFlatTree = false;
                    if (nodeName.indexOf('/') == 0) {
                        isFlatTree = true;
                        nodeName = nodeName.substring(nodeName.lastIndexOf('/') + 1);
                    }
                    var newNodeName = (self.type == 'file' ? filename : self.folder);
                    $http.post('/mock/data/rename', {
                        module: self.module,
                        oldPath: xUtil.path.join([self.parent, nodeName]),
                        newPath: xUtil.path.join([self.parent, newNodeName])
                    }).then(function(response) {
                        if (response.data == 'required') {
                            xDialog.alert('提示信息', '数据模块【' + self.module + '】不存在！');
                        } else if(response.data) {
                            if (self.type == 'file') {
                                toastr.success('文件【' + nodeName + '】已修改！');
                            } else {
                                toastr.success('文件夹【' + nodeName + '】已修改！');
                            }
                            if (isFlatTree) {
                                xContext.treeNode.name = xUtil.path.join([self.parent, newNodeName]);
                            } else {
                                xContext.treeNode.name = newNodeName;
                            }
                            $uibModalInstance.close(true);
                        } else if (response.status == 200) {
                            if (self.type == 'file') {
                                xDialog.alert('提示信息', '您重命名的文件已存在！');
                            } else {
                                xDialog.alert('提示信息', '您重命名的文件夹已存在！');
                            }
                        }
                    });
                }
            }
        }

        self.cancel = function() {
            $uibModalInstance.dismiss();
        }
    });
});
